<?php

/* Classe helper de autenticação
 * Method Chaining
 * 
 *  */

    class authHelper {
        
    protected   $sessionHelper, $redirectorHelper , $tableName, $userColumn , 
                $passColumn , $user , $pass, $loginController = 'index', 
                $loginAction = 'index',
                $logoutController = 'index', $logoutAction = 'index';
     

    public function __construct(){
        $this->sessionHelper = new SessionHelper();
        $this->redirectorHelper = new redirectorHelper();
        return $this;
    }
    
    public function setTableName( $valor ){
        $this->tableName = $valor;
        return $this;
    }
    
    public function setUserColumn( $valor ){
        $this->userColumn = $valor;
        return $this;
    }
    
    public function setPassColumn( $valor ){
        $this->passColumn = $valor;
        return $this;
    }
    
    public function setUser( $valor ){
        $this->user = $valor;
        return $this;
    }
    
    public function setPass( $valor ){
        $this->pass = $valor;
        return $this;
    }
    
    public function setLoginControllerAction ( $controller , $action ) {
        $this->loginController = $controller;
        $this->loginAction = $action;
        return $this;
    }
    
    public function setLogoutControllerAction ( $controller, $action ){
        $this->logoutController = $controller;
        $this->loginAction = $action;
        return $this;
    }
    
    public function login(){
        
        $db = new AdminModel();
        $db->_tabela = 'usuarios';
        //where = (ColunaNome DB) = nomeUsuario and (Colunasenha DB) = senha
      $where = $this->userColumn. "='" . $this->user ."' and ". $this->passColumn. "='".$this->pass."'";
      $sql = $db->select($where, 1);
    // print_r($sql[0]);
       count($sql);
       
       
        if (count($sql)>0){
            $this->sessionHelper->createSession("userAuth", true)
                                ->createSession("userData", $sql[0]);}
        
        else
            die();
           // header("Location: ".BASE."/admin/login=".$_POST);   
        ($this->redirectorHelper->goToControllerAction($this->loginController, $this->loginAction));
        
     return $this;
    }
    
    
    public function logout(){
        $this->sessionHelper->DeleteSession('userAuth')
                            ->DeleteSession('userAuth')
                            ->deleteCookie('base');
        $this->redirectorHelper->goToControllerAction($this->logoutController, $this->logoutAction);
        return $this;
    }
    
    public function checkLogin( $action ){
        switch ($action){
            case "boolean":
                if(!$this->sessionHelper->checkSession("userAuth"))
                    return false;
                else
                    return true;
                break;
                
            case "redirect":   
                if ( !$this->sessionHelper->checkSession("userAuth") )
                    if ( $this->redirectorHelper->getCurrentController() != $this->loginController || $this->redirectorHelper->getCurrentAction() != $this->loginAction)
                        $this->redirectorHelper->goToControllerAction($this->loginController, $this->loginAction);
                    
                    break;
            
            case "stop":
                if(!$this->sessionHelper->checkSession("userAuth"))
                    exit;
                 break;
        }
    }
        public function userData( $key ){
            $s = $this->sessionHelper->selectSession('userData');
            return $s[$key];
            
        }
    }
?>
